package com.applet.applet_shangcheng.service;

import com.applet.applet_shangcheng.dao.CashDiscountMapper;
import com.applet.applet_shangcheng.dao.UserMapper;
import com.applet.applet_shangcheng.entity.CashDiscount;
import com.applet.applet_shangcheng.utlis.AppletUtil;
import com.applet.applet_shangcheng.utlis.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.aop.config.AopConfigUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CashDiscountService {
    @Autowired
    private CashDiscountMapper cashDiscountMapper;

    @Autowired
    private UserMapper userMapper;

    public int insertCashDiscount(CashDiscount cashDiscount){
        String code = cashDiscount.gettCode();
        if (StringUtils.isEmpty(code)){
            cashDiscount.settCode(AppletUtil.getRandomStringByLength(18));
        }
        cashDiscount.setCreateTime(new Date());
        cashDiscount.setStatus(1);
        return cashDiscountMapper.insertCashDiscount(cashDiscount);

    };

    public Map updateCashDiscount(String code, int userId){
        Map<String,Object> map = new HashMap<>();

        CashDiscount cashDiscount = cashDiscountMapper.selectCashDiscountByCode(code);
        cashDiscount.setUserId(userId);
        cashDiscount.setStatus(0);
        cashDiscount.setEmployTime(new Date());
        int integral = cashDiscount.getIntegral();
        if (integral!=0){
            int result = cashDiscountMapper.updateCashDiscount(cashDiscount);
            userMapper.addIntrgral(integral,userId);
            map.put("addIntrgral","兑换成功!获取: "+integral+" 积分");
            return map;
        }
        double money = cashDiscount.getMoney();
        if (money!=0){
            int result = cashDiscountMapper.updateCashDiscount(cashDiscount);
            userMapper.addMoney1(money,userId);
            map.put("addIntrgral","兑换成功!获取: "+money+" 元");
            return map;
        }
        return map;
    };

    public  List<CashDiscount>  selectCashDiscountByUserId( String userId){
        List<CashDiscount>  cashDiscount = cashDiscountMapper.selectCashDiscountByUserId(userId);
        return cashDiscount;
    };

    public  CashDiscount  selectCashDiscountByCode(String code){
        CashDiscount cashDiscount = cashDiscountMapper.selectCashDiscountByCode(code);
        return cashDiscount;
    };

    public List<CashDiscount> findAll() {
        return cashDiscountMapper.findAll();
    }

    public PageResult findAll1(String queryString, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize );
        Page<CashDiscount> page = cashDiscountMapper.findAll1(queryString);
        //PageInfo<Announcement> pageInfo = new PageInfo<>(announcementList);
        return new PageResult(page.getTotal(),page.getResult() );
    }

    public CashDiscount findById(int id) {
        return cashDiscountMapper.findById(id);
    }

    public int updateCashDiscountByCode(CashDiscount cashDiscount) {
        return cashDiscountMapper.updateCashDiscount(cashDiscount);
    }

    public int deleteCashDiscountByCode(String code) {
        return cashDiscountMapper.deleteCashDiscountByCode(code);
    }
}
